/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package izsla.beans;

import izsla.entities.News;
import izsla.entities.Participate;
import izsla.entities.Trip;
import izsla.entities.TripUser;
import izsla.sb.TripManagementLocal;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Date;
import javax.ejb.EJB;
import javax.faces.context.FacesContext;

/**
 *
 * @author aki
 */
public class TripDetails {
    // REQ8

    @EJB
    private TripManagementLocal tm;

    private Trip trip = null;

    private String newsContent;

    private long newsId = -1;

    private String error = "";


    public TripDetails() {
    }

    public String getError() {
        // return ((!isUserJoined()) && (trip.getLimit() == trip.getParticipants().size()))?"Sorry the trip is full.":"";
        return error;
    }

    public String showTrip() {
	String tmp = FacesContext.getCurrentInstance().getExternalContext().getRequestParameterMap().get("tripId");
	long id = -1;
	try {
	    id = Long.parseLong(tmp);
	}
	catch(Exception e) {
	}

	trip = tm.getTrip(id);
        error = "";

	return "showtrip";
    }

    public String onAddNews() {
        // REQ12
	News news = new News();

	news.setContent(newsContent);
	news.setCreated(new Date());
	news.setTrip(trip);

	TripUser user = (TripUser)FacesContext.getCurrentInstance().getExternalContext().getSessionMap().get("user");
	news.setPublisher(user);

	Collection<News> nl = trip.getNewsItem();

	if(nl == null) {
	    nl = new ArrayList<News>();
	    trip.setNewsItem(nl);
	}
	nl.add(news);

	tm.mergeTrip(trip);

	return null;
    }

    public boolean isUserJoined() {
	return tm.isParticipate(SessionHelper.getUser(), trip);
    }

    /**
     * Joint the user to the trip, if the limit is not reached.
     */
    public String onJoin() {
        // REQ9
        if(trip.getLimit() == trip.getParticipants().size()) {
            error = "Sorry, the trip is full.";
            return null;
        } else
            error = "";

	TripUser user = SessionHelper.getUser();
	Participate part = new Participate(trip, user);

	trip.getParticipants().add(part);
	user.getTrips().add(part);

	tm.mergeTrip(trip);

	return null;
    }

    /**
     * Unregister a user from the trip. It is the inverse of join.
     */
    public String onCancelTrip() {
        // REQ10
	TripUser user = SessionHelper.getUser();
	Participate part = null;

	for(Participate p: user.getTrips()) {
	    if(p.getParticipant().equals(user) && p.getTrip().equals(trip)) {
		part = p;
		break;
	    }
	}

	if(part != null) {
	    trip.getParticipants().remove(part);
            user.getTrips().remove(part);
	    tm.deleteParticipation(part);
            tm.mergeTrip(trip);
	}

	return null;
    }

    /**
     *
     * @return true, iff the user has permission to edit the informations.
     * This is when the user is the organiser, or is an admin.
     */
    public boolean getUserEditable() {
	if(trip == null) {
	    return false;
	}

	TripUser user = (TripUser)FacesContext.getCurrentInstance().getExternalContext().getSessionMap().get("user");

        if(user.getIsAdmin() == true)
            return true;

	return user.equals(trip.getOrganiser());
    }

    public boolean getHasMap() {
	if(trip == null) {
	    return false;
	}

	return trip.getMapURL().length() > 0;
    }

    public String onDeleteNews() {
	tm.deleteNews(newsId);
	trip = tm.getTrip(trip.getId());
	return null;
    }


    public Trip getTrip() {
	return trip;
    }

    public void setTrip(Trip trip) {
	this.trip = trip;
    }

    public String getNewsContent() {
	return newsContent;
    }

    public void setNewsContent(String newsContent) {
	this.newsContent = newsContent;
    }

    public long getNewsId() {
	return newsId;
    }

    public void setNewsId(long newsId) {
	this.newsId = newsId;
    }
}
 
